package ar.com.bdf.ipharma.protocolo.model;

import ar.com.bdf.ipharma.protocolo.model.exceptions.InvalidHandShakeException;

public class HandShake {

	private Trama request;
	private Trama responseOk;
	private Trama responseFail;
	
	private ParserTrama parser = new ParserTrama();

	public HandShake(Trama request, Trama response) {
		super();
		this.request = request;
		this.responseOk = response;
	}

	public HandShake(Trama req, Trama respOk, Trama respFail) {
		this(req, respOk);
		this.responseFail = respFail;
	}

	public Trama getRequest() {
		return request;
	}

	public Boolean handshakeSuccess(Trama responseReal) {
		
		if (responseOk.equals(responseReal)){
			return true;
		}else if (responseFail != null && responseFail.equals(responseReal)){
			return false;
		}
		
		String errorMsg = "La respuesta del handshake es invalida, se esparaba como confirmacion ";
		errorMsg = parser.serializable(responseOk);
		
		if (responseFail != null){
			errorMsg = " o " + parser.serializable(responseFail);
		}
		
		throw new InvalidHandShakeException(errorMsg);
	}

	public Trama getResponseOk() {
		return responseOk;
	}

	public void setResponseOk(Trama responseOk) {
		this.responseOk = responseOk;
	}

	public Trama getResponseFail() {
		return responseFail;
	}

	public void setResponseFail(Trama responseFail) {
		this.responseFail = responseFail;
	}
	
	
}
